// ActionScript file
	import mx.controls.*;
	import com.flexblocks.skins.FBPanelBgSkin;
	import mx.effects.easing.Bounce;
	import mx.events.FlexEvent;
	import mx.collections.ArrayCollection;
	import com.rclabs.flex.utils.finq.FinqObj;
	import com.rclabs.flex.utils.finq.IEnumerable;
	import mx.managers.PopUpManager;
	import mx.collections.ArrayCollection;
	
	import mx.events.ListEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.utils.ObjectUtil;
	
	private static var LOAD_DELAY:int = 6;
	private static var timeElapsed:int = 0;
	private var loadTimer:Timer;
	private static var img:Image = new Image();

	[Bindable] public var xml:XML;
	[Bindable] public var templeXMLList:XMLList;			
	[Bindable] public var yahooGeocodeResponseXMLList:XMLList;
	[Bindable] public var templeArrayCollection:ArrayCollection;
	[Bindable] public var templeResultSetArrayCollection:ArrayCollection;
	[Bindable] public var listArrayCollection:ArrayCollection;
	[Bindable] public var cityArrayCollection:ArrayCollection;
	[Bindable] public var selectedCity:String
	public var filteredCityArrayCollection:ArrayCollection;
	
	private var keys:Object = {};
	private var filterProperty:String = "city";
	
	private var cityArray:Array;
	private var filteredCityArray:Array;
	private var selectedState:String;
	private var city:String;
	private var templeEnum:IEnumerable;
	private var templeResultsEnum:IEnumerable;
	private var templeEnumArrayCollection:ArrayCollection;
	private var templeData:String;
	private var tempCityArrayCollection:ArrayCollection;
	private var templeEnumResultSetArrayCollection:ArrayCollection;

	[Bindable]
	[Embed(source="assets/flagicons/UnitedStates.png")]
	public var UnitedStates_FlagIcon:Class;

	[Bindable]
	[Embed(source="assets/flagicons/Canada.png")]
	public var Canada_FlagIcon:Class;
	
	[Bindable]
	public var unitedstates_statesArrayCollection:ArrayCollection = new ArrayCollection(
	  [ {label:"Alabama (AL)", data:"AL"}, 
		{label:"Alaska (AK)", data:"AK"}, 
		{label:"Arizona (AZ)", data:"AZ"},
		{label:"Arkansas (AR)", data:"AR"},
		{label:"California (CA)", data:"CA"},
		{label:"Colorado (CO)", data:"CO"},
		{label:"Connecticut (CT)", data:"CT"},
		{label:"Delaware (DE)", data:"DE"},
		{label:"District of Columbia (DC)", data:"DC"},
		{label:"Florida (FL)", data:"FL"},
		{label:"Georgia (GA)", data:"GA"},
		{label:"Hawaii (HI)", data:"HI"},
		{label:"Idaho (ID)", data:"ID"},
		{label:"Illinois (IL)", data:"IL"},
		{label:"Indiana (IN)", data:"IN"},
		{label:"Iowa (IA)", data:"IA"},
		{label:"Kansas (KS)", data:"KS"},
		{label:"Kentucky (KY)", data:"KY"},
		{label:"Louisiana (LA)", data:"LA"},
		{label:"Maine (ME)", data:"ME"},
		{label:"Maryland (MD)", data:"MD"},
		{label:"Massachusetts (MA)", data:"MA"},
		{label:"Michigan (MI)", data:"MI"},
		{label:"Minnesota (MN)", data:"MN"},
		{label:"Mississipi (MS)", data:"MS"},
		{label:"Missouri (MO)", data:"MO"},
		{label:"Montana (MT)", data:"MT"},
		{label:"Nebraska (NE)", data:"NE"},
		{label:"Nevada (NV)", data:"NV"},
		{label:"New Hampshire (NH)", data:"NH"},
		{label:"New Jersey (NJ)", data:"NJ"},
		{label:"New Mexico (NM)", data:"NM"},
		{label:"New York (NY)", data:"NY"},
		{label:"North Carolina (NC)", data:"NC"},
		{label:"North Dakota (ND)", data:"ND"},
		{label:"Ohio (OH)", data:"OH"},
		{label:"Oregon (OR)", data:"OR"},
		{label:"Pennsylvania (PA)", data:"PA"},
		{label:"Rhode Island (RI)", data:"RI"},
		{label:"South Carolina (SC)", data:"SC"},
		{label:"South Dakota (SD)", data:"SD"},
		{label:"Tennessee (TN)", data:"TN"},
		{label:"Texas (TX)", data:"TX"},
		{label:"Utah (UT)", data:"UT"},
		{label:"Vermont (VT)", data:"VT"},
		{label:"Washington (WA)", data:"WA"},
		{label:"West Virginia (WV)", data:"WV"},
		{label:"Wisconsin (WI)", data:"WI"},
		{label:"Wyoming (WY)", data:"WY"}]);

	[Bindable]
	public var canada_statesArrayCollection:ArrayCollection = new ArrayCollection(
	  [ {label:"Alberta (AB)", data:"AB"},
		{label:"British Columbia (BC)", data:"BC"},
		{label:"Manitoba (MB)", data:"MB"},
		{label:"Newfoundland and Labrador (NL)", data:"NL"},
		{label:"New Brunswick (NB)", data:"NB"},
		{label:"Northwest Territories (NT)", data:"NT"},
		{label:"Nova Scotia (NS)", data:"NS"},
		{label:"Nunavut (NU)", data:"NU"},
		{label:"Ontario (ON)", data:"ON"},
		{label:"Prince Edward Island (PE)", data:"PE"},
		{label:"Quebec (QC)", data:"QC"},
		{label:"Saskatchewan (SK)", data:"SK"},      
		{label:"Yukon (YK)", data:"YK"}]);
	
	private function init():void {
		changeToState("initial");
		showSplashScreen();
	}

	public function changeToState(STATE:String):void{
		if(STATE=="initial"){
			resetButton.label = "Reset Selection";
			searchResultsButton.label = "Search Results";
			
			resetButton.visible = false;
			searchResultsButton.visible = false;
			
			countryComboBox.selectedIndex = -1;
			stateComboBox.selectedIndex = -1;
			cityComboBox.selectedIndex = -1;
			
			stateComboBox.enabled = false;
			cityComboBox.enabled = false;
			
			mainPanel.height = 350;
			mainPanel.width = 600;
			
			mainPanelViewStack.selectedIndex = 0;
		}
		else if(STATE=="results"){
			resetButton.label = "New Search";
			resetButton.visible = true;
			searchResultsButton.visible = false;
			
			mainPanel.height = 500;
			mainPanel.width = 800;
			
			mainPanelViewStack.selectedIndex = 1;
		}
	}

	private function showSplashScreen():void {
		img.addEventListener(MouseEvent.CLICK, img_click);
		img.addEventListener(Event.COMPLETE, img_complete);
		img.load('assets/images/nahts_splash.png');
		PopUpManager.addPopUp(img, this, true);
		loadTimer = new Timer(1000);
		loadTimer.addEventListener(TimerEvent.TIMER, incrementTime);
		loadTimer.start();
	}

	private function img_complete(evt:Event):void {
		img = evt.currentTarget as Image;
		img.width = img.contentWidth;
		img.height = img.contentHeight;
		PopUpManager.centerPopUp(img);
	}
	
	private function img_click(evt:MouseEvent):void {
		img = evt.currentTarget as Image;
		PopUpManager.removePopUp(img);
		postSplashInitialization();
	}
	
	private function incrementTime(event:TimerEvent):void
	{
		timeElapsed++;
		
		// close the splash screen, and open the document window
		if ((LOAD_DELAY - timeElapsed) == 0)
		{
			loadTimer.stop();
			loadTimer.removeEventListener(TimerEvent.TIMER, incrementTime);
			loadTimer = null;
			
			PopUpManager.removePopUp(img);
			postSplashInitialization();	
		}
	}

	private function postSplashInitialization():void
	{
		//Application.application.setStyle( "backgroundImage", "http://farm4.static.flickr.com/3292/2342380918_d4c8a29e15_b.jpg" );	
		loadTempleData();
	}
	
	private function loadTempleData():void{
		var request:URLRequest = new URLRequest("templedb/templedatabase.xml");
		var loader:URLLoader = new URLLoader();
		loader.addEventListener(Event.COMPLETE, loadTempleDataCompleteHandler);
		try
		{
			loader.load(request);
		}
		catch (error:ArgumentError)
		{
			Alert.show("An ArgumentError has occurred.");
		}
		catch (error:SecurityError)
		{
			Alert.show("A SecurityError has occurred.");
		}
	}
	
	private function loadTempleDataCompleteHandler(event:Event):void
	{
		var xml:XML = XML(event.target.data);
		
		templeArrayCollection = new ArrayCollection();
		templeResultSetArrayCollection = new ArrayCollection();
		
		templeEnumArrayCollection = new ArrayCollection();
		templeEnumResultSetArrayCollection = new ArrayCollection();
		
		cityArrayCollection = new ArrayCollection();
		tempCityArrayCollection = new ArrayCollection();
		
		templeXMLList = xml.temple as XMLList;
		createTempleArrayCollection();
	}
	
	private function createTempleArrayCollection():void{
		var n:int = templeXMLList.length();
		
		for(var i:int=0 ; i<n ; i++)
		{
			templeArrayCollection.addItem({"name":templeXMLList[i].name,
				"street":templeXMLList[i].street,
				"city":templeXMLList[i].city,
				"state":templeXMLList[i].state,
				"zip":templeXMLList[i].zip,
				"phone1":templeXMLList[i].phone1,
				"phone2":templeXMLList[i].phone2,
				"email":templeXMLList[i].email,
				"website":templeXMLList[i].website}); 
			
			templeResultSetArrayCollection.addItem({"name":templeXMLList[i].name,
				"street":templeXMLList[i].street,
				"city":templeXMLList[i].city,
				"state":templeXMLList[i].state,
				"zip":templeXMLList[i].zip,
				"phone1":templeXMLList[i].phone1,
				"phone2":templeXMLList[i].phone2,
				"email":templeXMLList[i].email,
				"website":templeXMLList[i].website});
		}
		
		templeEnum = new FinqObj();
		
		for(var k:int=0; k<templeArrayCollection.length; k++){
			templeEnum.push({name:""+templeArrayCollection.getItemAt(k).name,
				street:""+templeArrayCollection.getItemAt(k).street,
				city:""+templeArrayCollection.getItemAt(k).city,
				state:""+templeArrayCollection.getItemAt(k).state,
				zip:""+templeArrayCollection.getItemAt(k).zip,
				phone1:""+templeArrayCollection.getItemAt(k).phone1,
				phone2:""+templeArrayCollection.getItemAt(k).phone2,
				email:""+templeArrayCollection.getItemAt(k).email,
				website:""+templeArrayCollection.getItemAt(k).website});
		}	
	}